// import { menuMap } from '@/config/menu.config';

// const permission = {
//   state: {
//     allMenuMap: [],
//     allowedMenuMap: [],
//     status: 0 // 0初始状态  1加载过权限
//   },
//   mutations: {
//     SET_ALLMENUMAP: (state, menuMap) => {
//       state.allMenuMap = menuMap;
//     },
//     SET_ALLOWEDMENUMAP: (state, menuMap) => {
//       state.allowedMenuMap = menuMap;
//     }
//   },
//   actions: {
//     InitMenu ({ commit }, funclist) {
//       return new Promise(resolve => {
//         commit('SET_ALLMENUMAP', menuMap);

//         let filterdMenuMap = [];
//         menuMap.forEach(menu => {
//           let filterdMenuChildren = menu.nodes.filter(nodesMenu => {
//             let permission = funclist.filter(item => {
//               if (item.funcId === nodesMenu.link.substring(1)) {
//                 nodesMenu.inEdit = item.inEdit != '' ? item.inEdit : '';
//                 return true
//               } else {
//                 return false
//               }
//             })
//             if (permission.length > 0) {
//               return true;
//             } else {
//               return false;
//             }
//           });
//           if (filterdMenuChildren.length !== 0 || menu.link !== '') {
//             filterdMenuMap.push({
//               label: menu.label,
//               icon: menu.icon,
//               link: menu.link,
//               inEdit: 'R',
//               nodes: filterdMenuChildren
//             });
//           }
//         });

//         commit('SET_ALLOWEDMENUMAP', filterdMenuMap);
//         resolve();
//       });
//     },

//     RenewMenu ({ commit }, funclist) {
//       return new Promise(resolve => {
//         // const filterdMenuMap = [];
//         // this.state.permission.allMenuMap.forEach(menu => {
//         //   let filterdMenuChildren = menu.children.filter(childrenMenu => {
//         //     let permissions = childrenMenu.permission.filter(permission => {
//         //       if (groups.includes(permission)) {
//         //         return true;
//         //       } else {
//         //         return false;
//         //       }
//         //     });
//         //     if (permissions.length > 0) {
//         //       return true;
//         //     } else {
//         //       return false;
//         //     }
//         //   });
//         //   if (filterdMenuChildren.length !== 0) {
//         //     filterdMenuMap.push({
//         //       title: menu.title,
//         //       backgroundImage: menu.backgroundImage,
//         //       children: filterdMenuChildren
//         //     });
//         //   }
//         // });
//         let filterdMenuMap = [];
//         menuMap.forEach(menu => {
//           let filterdMenuChildren = menu.nodes.filter(nodesMenu => {
//             let permission = funclist.filter(item => {
//               if (item.MANHY === nodesMenu.link.substring(1)) {
//                 nodesMenu.inEdit = item.inEdit != '' ? item.inEdit : '';
//                 return true
//               } else {
//                 return true
//               }
//             })
//             if (permission.length > 0) {
//               return true;
//             } else {
//               return false;
//             }
//           });
//           if (filterdMenuChildren.length !== 0) {
//             filterdMenuMap.push({
//               label: menu.label,
//               icon: menu.icon,
//               link: menu.link,
//               nodes: filterdMenuChildren
//             });
//           }
//         });

//         commit('SET_ALLOWEDMENUMAP', filterdMenuMap);
//         resolve();
//       });
//     }
//   }
// };
// export default permission;

import { asyncRouterMap, constantRouterMap } from '@/config/router.config'

/**
 * 过滤账户是否拥有某一个权限，并将菜单从加载列表移除
 *
 * @param permission
 * @param route
 * @returns {boolean}
 */
function hasPermission (permission, route) {
  if (route.meta && route.meta.permission) {
    let flag = -1
    for (let i = 0, len = permission.length; i < len; i++) {
      flag = route.meta.permission.indexOf(permission[i])
      if (flag >= 0) {
        return true
      }
    }
    return false
  }
  return true
}

/**
 * 单账户多角色时，使用该方法可过滤角色不存在的菜单
 *
 * @param roles
 * @param route
 * @returns {*}
 */
// eslint-disable-next-line
function hasRole (roles, route) {
  if (route.meta && route.meta.roles) {
    return route.meta.roles.indexOf(roles.id)
  } else {
    return true
  }
}

function filterAsyncRouter (routerMap, roles) {
  const accessedRouters = routerMap.filter(route => {
    if (hasPermission(roles.permissionList, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles)
      }
      return true
    }
    return false
  })
  return accessedRouters
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, data) => {
      state.addRouters = data
      state.routers = constantRouterMap.concat(data)
    }
  },
  actions: {
    GenerateRoutes ({ commit }, data) {
      return new Promise(resolve => {
        const { roles } = data
        console.log(roles)
        let accessedRouters
        // eslint-disable-next-line prefer-const
        accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    },
    // 动态添加主界面路由，需要缓存
    UpdateAppRouter ({ commit }, routes) {
      return new Promise(resolve => {
        const routelist = routes.constRoutes
        commit('SET_ROUTERS', routelist)
        resolve(routelist)
      })
    },
    // 动态添加主界面路由，需要缓存
    UpdatePageRouter ({ commit }, routes) {
      return new Promise(resolve => {
        const routelist = routes.contentPages
        commit('SET_ROUTERS', routelist)
        resolve(routelist)
      })
    }
  }
}

export default permission
